package base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

public class Test1 {

    //用户按比例分堆
    public static void main(String[] args) {
        //分堆比例
        String proportion = "2:1:1";
        //分堆数
        String[] dumps = proportion.split(":");
        Object[] array = getArray(dumps);
        System.out.println("array : "+JSONObject.toJSONString(array));
        Map<Integer, List<Long>> map = getMap(dumps.length);
        System.out.println("map : "+JSONObject.toJSONString(map));
        for(long i = 0; i <= 11; i ++){
            int num = num(array, i);
            map.get(array[num]).add(i);
        }
        System.out.println(JSONObject.toJSONString(map));
    }

    public static Object[] getArray(String[] strArr){
        List<Integer> list = new LinkedList<>();
        for(int i = 0; i< strArr.length; i++){
            for(int j = 0; j < Integer.valueOf(strArr[i]);j++){
                list.add(i + 1);
            }
        }
        return list.toArray();
    }

    public static Map<Integer, List<Long>> getMap(int dump){
        Map<Integer, List<Long>> map = new HashMap<>();
        for(int i = 1; i <= dump; i++){
            map.put(i, new ArrayList<>());
        }
        return map;
    }

    public static int num(Object[] array, Long userId){
      int index = Long.valueOf(userId %  array.length).intValue();
      System.out.println("userId : " + userId +" num : " + array[index]);
      return index;
    }
}
